<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>员工管理 - 员工信息管理系统</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
</head>
<body>
<div class="container">
    <header class="header">
        <h1>员工信息管理系统</h1>
        <div class="user-info">
            欢迎, ${user.username} |
            <a href="${pageContext.request.contextPath}/logout">退出</a>
        </div>
    </header>

    <nav class="sidebar">
        <ul>
            <li><a href="${pageContext.request.contextPath}/dashboard">仪表板</a></li>
            <li><a href="${pageContext.request.contextPath}/employee" class="active">员工管理</a></li>
        </ul>
    </nav>

    <main class="main-content">
        <div class="page-header">
            <h2>员工管理</h2>
            <a href="${pageContext.request.contextPath}/employee?action=add" class="btn btn-primary">添加员工</a>
        </div>

        <c:if test="${not empty success}">
            <div class="alert alert-success">
                    ${success}
            </div>
        </c:if>

        <c:if test="${not empty error}">
            <div class="alert alert-error">
                    ${error}
            </div>
        </c:if>

        <!-- 搜索表单 -->
        <div class="search-form">
            <form action="${pageContext.request.contextPath}/employee" method="get">
                <div class="form-row">
                    <div class="form-group">
                        <label for="name">姓名:</label>
                        <input type="text" id="name" name="name" value="${param.name}">
                    </div>
                    <div class="form-group">
                        <label for="department">部门:</label>
                        <select id="department" name="department">
                            <option value="">全部</option>
                            <option value="技术部" ${param.department == '技术部' ? 'selected' : ''}>技术部</option>
                            <option value="市场部" ${param.department == '市场部' ? 'selected' : ''}>市场部</option>
                            <option value="人事部" ${param.department == '人事部' ? 'selected' : ''}>人事部</option>
                            <option value="财务部" ${param.department == '财务部' ? 'selected' : ''}>财务部</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">搜索</button>
                        <a href="${pageContext.request.contextPath}/employee" class="btn btn-secondary">重置</a>
                    </div>
                </div>
            </form>
        </div>

        <!-- 员工列表 -->
        <div class="table-container">
            <table class="data-table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>姓名</th>
                    <th>部门</th>
                    <th>职位</th>
                    <th>薪资</th>
                    <th>入职日期</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach var="employee" items="${employees}">
                    <tr>
                        <td>${employee.id}</td>
                        <td>${employee.name}</td>
                        <td>${employee.department}</td>
                        <td>${employee.position}</td>
                        <td>¥${employee.salary}</td>
                        <td>${employee.hireDate}</td>
                        <td class="actions">
                            <a href="${pageContext.request.contextPath}/employee?action=view&id=${employee.id}" class="btn btn-info">查看</a>
                            <a href="${pageContext.request.contextPath}/employee?action=edit&id=${employee.id}" class="btn btn-warning">编辑</a>
                            <a href="${pageContext.request.contextPath}/employee?action=delete&id=${employee.id}"
                               class="btn btn-danger"
                               onclick="return confirm('确定要删除这个员工吗？')">删除</a>
                        </td>
                    </tr>
                </c:forEach>

                <c:if test="${empty employees}">
                    <tr>
                        <td colspan="7" class="no-data">没有找到员工数据</td>
                    </tr>
                </c:if>
                </tbody>
            </table>
        </div>

        <!-- 分页 -->
        <c:if test="${totalPages > 1}">
            <div class="pagination">
                <c:if test="${currentPage > 1}">
                    <a href="${pageContext.request.contextPath}/employee?page=${currentPage - 1}&name=${param.name}&department=${param.department}">上一页</a>
                </c:if>

                <c:forEach begin="1" end="${totalPages}" var="i">
                    <c:choose>
                        <c:when test="${i == currentPage}">
                            <span class="current-page">${i}</span>
                        </c:when>
                        <c:otherwise>
                            <a href="${pageContext.request.contextPath}/employee?page=${i}&name=${param.name}&department=${param.department}">${i}</a>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>

                <c:if test="${currentPage < totalPages}">
                    <a href="${pageContext.request.contextPath}/employee?page=${currentPage + 1}&name=${param.name}&department=${param.department}">下一页</a>
                </c:if>
            </div>
        </c:if>
    </main>
</div>
</body>
</html>